package hkr.vfu.bluecenter.swipe_fragments;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    /**
     * A {@link android.support.v4.app.FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */

    private ArrayList<Fragment> activeFragments;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
        activeFragments = new ArrayList<Fragment>();
        activeFragments.add(BluetoothSelectFragment.newInstance(0));
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return activeFragments.get(position);
    }

    public void addBoxFragment(String mac) {
        BoxFragment boxFragment = BoxFragment.newInstance(mac, activeFragments.size() - 1);
        activeFragments.add(boxFragment);
        notifyDataSetChanged();
    }

    public void removeBoxFragment(int pos) {
        activeFragments.remove(pos);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return activeFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return activeFragments.get(position).getTag();
    }

    public void updateUI() {
        for (Fragment f : activeFragments) {
            if (f instanceof BluecenterFragment) {
                ((BluecenterFragment) f).updateUI();
            }
        }
    }
}
